home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / ccl110je.zip / EXAMPLE1.CPP < prev    next >
C/C++ Source or Header  |  1993-06-19  |  3KB  |  80 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE1.CPP: example program 1 for DOS coroutine library.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15.  
  16. #include <iostream.h>
  17. #include "coroutine.h"
  18.  
  19. //--------------------------------------------------------------------------
  20. //
  21. //      Class Example1.
  22. //
  23. //      This class is a coroutine which displays a message three times
  24. //      before terminating.
  25. //
  26. class Example1 : public Coroutine
  27. {
  28.   public:
  29.     Example1 (int n)        { num = n; }
  30.  
  31.   protected:
  32.     virtual void main ();   // code to be executed by coroutine
  33.  
  34.   private:
  35.     int num;                // coroutine identification number
  36. };
  37.  
  38. //--------------------------------------------------------------------------
  39. //
  40. //      Example1::main.
  41. //
  42. //      This is the code executed by each instance of class Example1.
  43. //      It displays a startup message, executes a loop three times to
  44. //      display a progress message, then finally displays a termination
  45. //      message.
  46. //
  47. void Example1::main ()
  48. {
  49.     cout << "Coroutine E" << num << " started\n";
  50.  
  51.     for (int i = 1; i <= 3; i++)
  52.     {   cout << "Coroutine E" << num << ", iteration " << i << "\n";
  53.         pause ();
  54.     }
  55.  
  56.     cout << "Coroutine E" << num << " finished\n";
  57. }
  58.  
  59. //--------------------------------------------------------------------------
  60. //
  61. //      The main program.
  62. //
  63. //      This just demonstrates creating and executing a set of coroutines.
  64. //      Three instances of class Example1 are created and started.  The main
  65. //      program then terminates.  Termination of the main program will not
  66. //      complete until the coroutines it declares have also completed.
  67. //
  68. void main ()
  69. {
  70.     Example1 e1 (1), e2 (2), e3 (3);
  71.  
  72.     if (!e1.run ())
  73.         cout << "Couldn't start e1\n";
  74.     if (!e2.run ())
  75.         cout << "Couldn't start e2\n";
  76.     if (!e3.run ())
  77.         cout << "Couldn't start e3\n";
  78.  
  79. }   //--- destructors called here: wait for coroutines to finish, then exit
  80.